<?php

namespace app\factory\logic;

use app\common\exception\LogicException;
use app\model\PigHealth;
use think\facade\Db;

class PigHealthLogic
{
    protected PigHealth $model;
    protected int $factory_id;

    public function __construct()
    {
        $this->model = new PigHealth();
        $this->factory_id = request()->factory_info['factory_id'];
    }

    /**
     * @Description 上报
     * @param int $Id
     * @param array $param
     * @time 2024/03/23 14:55:39
     * @return true
     * @author: zj
     */
    public function report(int $Id, array $param)
    {
        Db::startTrans();
        try {
            $exist = $this->model->checkOutIdIsExist($this->factory_id, $Id);
            if ($exist) {
                $rule = [
                    'PigId' => 'integer|>:0',
                    'Temperature' => 'integer|>:0',
                    'Heartrate' => 'integer|>:0',
                    'Sao2' => 'integer|>:0',
                    'Pi' => 'integer|>:0',
                    'Br' => 'integer|>:0',
                ];
                validate($rule)->check($param);

                $updateData = [];
                if ($param['PigId']) {
                    $updateData['pig_id'] = $param['PigId'];
                }
                if ($param['Temperature']) {
                    $updateData['temperature'] = $param['Temperature'];
                }
                if ($param['Heartrate']) {
                    $updateData['heart_rate'] = $param['Heartrate'];
                }
                if ($param['Sao2']) {
                    $updateData['sao2'] = $param['Sao2'];
                }
                if ($param['Pi']) {
                    $updateData['pi'] = $param['Pi'];
                }
                if ($param['Br']) {
                    $updateData['breathe'] = $param['Br'];
                }
                $updateData['update_record_time'] = $param['RecordTime'];
                $updateData['update_time'] = request()->time();

                $this->model->where('out_id', $Id)
                    ->where('delete_time', $this->model::IS_DELETE_NO)
                    ->update($updateData);
            } else {
                $rule = [
                    'PigId' => 'require|integer|>:0',
                    'Temperature' => 'require|integer|>:0',
                    'Heartrate' => 'require|integer|>:0',
                    'Sao2' => 'require|integer|>:0',
                    'Pi' => 'require|integer|>:0',
                    'Br' => 'require|integer|>:0',
                ];
                validate($rule)->check($param);

                $this->model->insert([
                    'out_id' => $Id,
                    'pig_id' => $param['PigId'],
                    'factory_id' => $this->factory_id,
                    'temperature' => $param['Temperature'],
                    'heart_rate' => $param['Heartrate'],
                    'sao2' => $param['Sao2'],
                    'pi' => $param['Pi'],
                    'breathe' => $param['Br'],
                    'record_time' => $param['RecordTime'],
                    'update_record_time' => $param['RecordTime'],
                    'create_time' => request()->time(),
                    'update_time' => request()->time()
                ]);
            }

            Db::commit();
        } catch (\Exception $exception) {
            Db::rollback();
            throw new LogicException($exception->getMessage());
        }

        return true;
    }

}